package qmm.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qmm.util.JdbcUtil;

@WebServlet("/CheckEmailServlet")
public class CheckEmailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public CheckEmailServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//设置请求字符集
		response.setCharacterEncoding("utf-8");//设置响应字符集
		String email = request.getParameter("email");
		
		try {
			Connection conn = JdbcUtil.getConnection();
			//写sql
			String sql = "select count(*) from t_user where email='"+email+"'";
			//创建st对象
			Statement st = conn.createStatement();
			//执行sql
			ResultSet rs = st.executeQuery(sql);
			//处理结果
			if(rs.next()){
				//解析一行
				int n = rs.getInt(1);//取第一列的值
				if(n>0){
					response.getWriter().write("邮箱重复，请换一个");//返回文本
				}				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
